// ValidationAspects
// Written by Mike Saunders
// Released under Microsoft Public License (Ms-PL)
using System;
using System.Collections.Generic;
using ValidationAspects.Core;
using ValidationAspects.Sdk;

namespace ValidationAspects
{
	public static class Emit<TEmit> where TEmit : class
	{
		private static readonly Dictionary<Type, Func<object, TEmit>> emitFuncs = new Dictionary<Type, Func<object, TEmit>>();

		public static void For<TValidator>(Func<TValidator, TEmit> emitFunc) where TValidator : IValidatorFactory
		{
			Enforce.IsNotNull(emitFunc, "emitFunc");
			emitFuncs[typeof(TValidator)] = o => emitFunc(Cast<TValidator>.From(o));
		}

		public static TEmit From(IValidatorFactory factory)
		{
			Enforce.IsNotNull(factory, "factory");

			Func<object, TEmit> emitFunc;
			return !emitFuncs.TryGetValue(factory.GetType(), out emitFunc) ? null : emitFunc(factory);
		}

		public static void Clear()
		{
			emitFuncs.Clear();
		}
	}
}